package com.glory.brepda.customView;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.glory.brepda.R;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

/**
 * FileName: CustomeLoadingDialog
 * Author: XJH
 * Date: 2020/9/23 16:39
 * Description:
 * History:
 */
public class CustomLoadingDialog extends AppCompatDialogFragment {
    private Object okgoTag;
    private String tips = "";

    public CustomLoadingDialog() {
    }

    public void show(FragmentManager manager, String tag) {
        if (!this.isAdded()) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }

    }

    public void onStart() {
        super.onStart();
        Dialog dialog = this.getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout((int)((double) FreeApi_StaticMembers.SCREEN_WIDTH * 0.3D), (int)((double)FreeApi_StaticMembers.SCREEN_WIDTH * 0.2D));
        }

    }

    public void onDestroyView() {
        super.onDestroyView();
    }

    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resID = R.layout.custom_blue_sky_dialog_loading;
        View view = inflater.inflate(resID, (ViewGroup)null);
        TextView tvLoadingTips = (TextView)view.findViewById(R.id.tvLoadingTips);
        tvLoadingTips.setText(this.tips.length() == 0 ? "正在加载中..." : this.tips);
        this.getDialog().getWindow().setGravity(17);
        this.getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
        this.getDialog().setCancelable(true);
        this.getDialog().setCanceledOnTouchOutside(false);
        this.getDialog().setOnKeyListener((dialog, keyCode, event) -> keyCode == 4);
        return view;
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public void setData(Object okgoTa) {
        this.okgoTag = this.okgoTag;
    }

    public void setData(Object okgoTag, String tips) {
        this.okgoTag = okgoTag;
        this.tips = tips;
    }
}
